<template>
    <div>
    <!--按照用户名搜索区域-->
   <div style="margin-top: 10px;display: flex;justify-content: center">
       <el-input v-model="keywords" placeholder="请输入搜索用户名"
                 @keydown.enter.native="searchHrInfo"
                 prefix-icon="el-icon-search" style="width: 400px;margin-right: 10px"></el-input>
       <el-button icon="el-icon-search" type="primary" @click="searchHrInfo">搜索</el-button>
   </div>
    <div class="hr-container">
        <el-card class="hr-card" v-for="(hr,index) in allHrs" :key="index">
            <div slot="header" class="clearfix">
                <span>{{hr.name}}</span>
                <el-button style="float: right; padding: 3px 0;color: #e30007" type="text" icon="el-icon-delete" @click="deleteHr(hr)"></el-button>
            </div>
            <div>
                <!--用户头像-->
                <div class="img-container">
                    <img :src="hr.userface" :alt="hr.name" :title="hr.name" class="userface-img">
                </div>
                <div class="userinfo-container">
                    <div>用户名:{{hr.name}}</div>
                    <div>手机号码:{{hr.phone}}</div>
                    <div>电话号码:{{hr.telephone}}</div>
                    <div>地址:{{hr.address}}</div>
                    <div>用户状态：
                        <el-switch
                                v-model="hr.enabled"
                                @change="enabledState(hr)"
                                active-text="启用"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                inactive-text="禁用"
                        >
                        </el-switch>

                    </div>
                    <div>用户角色
                        <el-tag type="success"  style="margin-right: 4px" v-for="(role,roleIndex) in hr.roles" :key="roleIndex">
                            {{role.nameZh}}
                        </el-tag>
                        <el-popover
                                placement="bottom"
                                title="标题"
                                width="200"
                                trigger="click"
                                @show="showPop(hr)"
                                @hide="hidePop(hr)"
                               >
                            <el-select v-model="selectedRoles" multiple placeholder="请选择角色">
                                <el-option
                                    v-for="(r,indexRole) in allRoles"
                                    :key="indexRole"
                                    :label="r.nameZh"
                                    :value="r.id"
                                ></el-option>
                            </el-select>
                            <el-button @click="initAllRoles"  slot="reference" type="text" icon="el-icon-more"></el-button>
                        </el-popover>
                    </div>
                </div>
            </div>
        </el-card>
    </div>
    </div>
</template>

<script>
    export default {
        name: "SysHr",
        data(){
            return{
                keywords:'',
                allHrs:[],
                allRoles:[],
                selectedRoles:[]
            }
        },
        mounted() {
            this.initAllHrs();
            },
        methods:{
            deleteHr(hr){
                this.$confirm('此操作将永久删除【' + hr.name + '】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //点击确定按钮回调事件，执行删除部门操作
                    this.deleteRequest("/sys/hr/"+hr.id).then(resp=>{
                        if (resp) {
                         this.initAllHrs();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            searchHrInfo(){
                this.initAllHrs();
            },
            enabledState(hr)
            {
                delete hr.roles;
                this.putRequest("/sys/hr/",hr).then(resp=>{
                    if(resp)
                    {
                        this.initAllHrs();
                    }
                })
            },
            hidePop(hr){
                let roles=[];
                Object.assign(roles,hr.roles);
                let flag=false;
                if(roles.length!=this.selectedRoles.length)
                {
                    flag=true;
                }
                else
                {
                    for(let i=0;i<roles.length;i++)
                    {
                        let roleInfo=roles[i];
                        for(let j=0;j<this.selectedRoles.length;j++)
                        {
                            let selectId=this.selectedRoles[j];
                            if(roleInfo==selectId)
                            {
                                roles.slice(i,1);
                                i--;
                                break;
                            }
                        }
                    }
                    if(roles.length!=0)
                    {
                        flag=true;
                    }
                }
                if(flag){
                    let url="/sys/hr/updateRoles?hrId="+hr.id;
                    this.selectedRoles.forEach(roleIds=>{
                        url+="&roleIds="+roleIds;
                    });
                    this.putRequest(url).then(resp=>{
                        if(resp)
                        {
                            this.initAllHrs();
                        }
                    });
                }

            },
            showPop(hr){
                let hrRoles=hr.roles;
                this.selectedRoles=[];
                hrRoles.forEach(role=>{
                    this.selectedRoles.push(role.id);
                });
                this.initAllRoles();
            },
            initAllRoles(){
                this.getRequest("/sys/hr/getAllRoles").then(resp=>{
                    if(resp)
                    {
                        this.allRoles=resp;
                    }
                })
            },
            initAllHrs(){
                this.getRequest("/sys/hr/?keywords="+this.keywords).then(resp=>{
                    if(resp)
                    {
                        this.allHrs=resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .userinfo-container div {
        font-size: 12px;
        color: #409eff;
    }

    .userinfo-container {
        margin-top: 20px;
    }

    .img-container {
        width: 100%;
        display: flex;
        justify-content: center;
    }

    .userface-img {
        width: 72px;
        height: 72px;
        border-radius: 72px;
    }

    .hr-container {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
    }

    .hr-card {
        width: 350px;
        margin-bottom: 20px;
    }
</style>